package com.macllvm.whale.ui;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.widget.ImageButton;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.content.Context;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.view.inputmethod.InputMethodManager;
import android.view.View.OnFocusChangeListener;
import android.util.Log;
import java.text.DateFormat;
import java.util.Observer;
import java.util.Observable;
import com.macllvm.whale.Event;
import com.macllvm.whale.model.BuddyManager;
import com.macllvm.whale.adapter.MessageAdapter;
import com.macllvm.whale.entity.Buddy;
import com.macllvm.whale.protocol.ProtocolController;
import com.macllvm.whale.ui.widget.FileExplorerPanel;
import com.macllvm.whale.R;

public class ChatActivity extends Activity implements View.OnClickListener, Observer {
    private static final String TAG = "ChatActivity";

    private ListView mMsgListView;
    private Button mBtnSend;
    private ImageButton mBtnBack;
    private Button mBbtnAttach;
    private Button mTopbarBtnBack;
    private EditText  mTextMessage;
    private TextView mBarTitle;
    private String targetIP;
    private Buddy buddy;
    private MessageAdapter mMsgAdapter;
    private BuddyManager buddyManager;
    private ProtocolController mController;
    private FileExplorerPanel mFileExplorerPanel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        mBtnSend = (Button)findViewById(R.id.btn_send);
        mBbtnAttach = (Button)findViewById(R.id.btn_attach);
        mTopbarBtnBack = (Button)findViewById(R.id.topbar_btn_back);
        mTextMessage = (EditText)findViewById(R.id.chat_input);
        mMsgListView = (ListView)findViewById(R.id.message_list);
        mBarTitle = (TextView)findViewById(R.id.topbar_title);
        mFileExplorerPanel = (FileExplorerPanel)findViewById(R.id.file_explorer_panel);

        mBtnSend.setOnClickListener(this);
        mBbtnAttach.setOnClickListener(this);
        mTopbarBtnBack.setOnClickListener(this);

        mController = ProtocolController.getInstance();
        Intent intent = this.getIntent();
        if (null != intent) {
            targetIP = intent.getExtras().getString("ip");
            buddy = mController.getBuddyManager().get(targetIP);
            buddy.resetUnreadCount();
            mFileExplorerPanel.setIP(targetIP);
            mController.setSessionIP(targetIP);
            mBarTitle.setText(buddy.getHostName());
        }
        
        mMsgAdapter = new MessageAdapter(this);
        mMsgAdapter.setDataSource(mController.getMessageManager().getDataSource(targetIP));
        mMsgListView.setAdapter(mMsgAdapter);

    }

    @Override
    public void onStart() {
        super.onStart();
        mController.getMessageManager().addObserver(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        mController.getMessageManager().deleteObserver(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mController.setSessionIP("");
    }

    @Override
    public void onBackPressed() {
        if(mFileExplorerPanel.getVisibility() == View.VISIBLE ) {
            mFileExplorerPanel.hide();
            return;
        }
        finish();
    }

    @Override
    public void update(Observable object, Object argument) {
        if(targetIP.equals((String)argument))
            handler.sendEmptyMessage(Event.EVENT_REFRESH_MESSAGE);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_send: {
                String msg = mTextMessage.getText().toString();
                if (null != msg && msg.length() > 0) {
                    mTextMessage.setText("");
                    mController.sendMessage(targetIP, msg);
                }
            }
            return;
            case R.id.topbar_btn_back: {
                finish();
            }
            return;
            case R.id.btn_attach: {
                cloeInputMethod();
                mFileExplorerPanel.show();
            }
            default:
                return;
        }
    }

    private void cloeInputMethod() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean isOpen = imm.isActive();
        if (isOpen) {
            imm.hideSoftInputFromWindow(mTextMessage.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
        switch (msg.what) {
            case Event.EVENT_REFRESH_MESSAGE:
                mMsgAdapter.notifyDataSetChanged();
                break;
        }
        }
    };
}
